package com.farmproduct.service;

import com.farmproduct.dao.BackMesMapper;
import com.farmproduct.pojo.BackMes;
import com.farmproduct.pojo.BackMesExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class BackMesService {
    @Autowired
    private BackMesMapper backMesMapper;
    @Autowired
    private BackMesExample backMesExample;

    /**
     * 添加故障单
     * */
    public boolean addBackMes(BackMes backMes){
        backMesExample.clear();
        if (backMesMapper.insert(backMes)==1){
            return true;
        }
        return false;
    }

    /**
     * 删除故障单
     * */
    public boolean delBackMes(BackMes backMes){
        backMesExample.clear();
        if (backMesMapper.deleteByPrimaryKey(backMes.getBid())==1){
            return true;
        }
        return false;
    }
    public boolean delBackMesList(List<BackMes> backMes){
        if (backMes.size()>0){
            for (BackMes backMes1 : backMes) {
                if(backMesMapper.deleteByPrimaryKey(backMes1.getBid())!=1){
                    return false;
                }
            }return true;

        }
        return false;
    }

    /**
     * 更新故障单
     * */
    public boolean updateBackMes(BackMes backMes){
        backMesExample.clear();
        if (backMesMapper.updateByPrimaryKey(backMes)==1){
            return true;
        }
        return false;
    }

    public boolean updateBackMesStatus(BackMes backMes){
        backMesExample.clear();
        backMes.setStatus(1);
        if (backMesMapper.updateByPrimaryKey(backMes)==1){
            return true;
        }
        return false;
    }
    public boolean updateBackMesList(List<BackMes> backMes){
        if (backMes!=null){
            for (BackMes backMes1 : backMes) {
                backMes1.setStatus(1);
                if(backMesMapper.updateByPrimaryKey(backMes1)!=1){
                    return false;
                }
            }return true;

        }
        return false;
    }

    public List<BackMes> getBackMesByUid(String uid){
        backMesExample.clear();
        backMesExample.createCriteria().andUidEqualTo(uid);
        List<BackMes> backMes = backMesMapper.selectByExample(backMesExample);
        if (backMes!=null){
            return backMes;
        }
        return null;
    }

    public List<BackMes> getBackMesByUidNo(String  uid){
        backMesExample.clear();
        backMesExample.createCriteria().andUidEqualTo(uid).andStatusEqualTo(0);
        List<BackMes> backMes = backMesMapper.selectByExample(backMesExample);
        if (backMes!=null){
            return backMes;
        }
        return null;
    }
    public List<BackMes> getBackMesByUidYes(String  uid){
        backMesExample.clear();
        backMesExample.createCriteria().andUidEqualTo(uid).andStatusEqualTo(1);
        List<BackMes> backMes = backMesMapper.selectByExample(backMesExample);
        if (backMes!=null){
            return backMes;
        }
        return null;
    }


    public long getNoConuntByUid(String  uid){
        backMesExample.clear();
        backMesExample.createCriteria().andStatusEqualTo(0).andUidEqualTo(uid);
        return backMesMapper.countByExample(backMesExample);
    }
    public long getYesConuntByUid(String uid){
        backMesExample.clear();
        backMesExample.createCriteria().andStatusEqualTo(1).andUidEqualTo(uid);
        return backMesMapper.countByExample(backMesExample);
    }

}
